import { clear } from 'console';
export class AppendElement {
  timer: number;
  nowTime: number = new Date().getTime();
  httpRequestTimer: number;
  Div: Element;
  date: Date = new Date();
  constructor() {
  };
  public openLoading(timeout: Number = 0) {
    const loading = document.getElementById('loading');
    if (loading) {
      return;
    }
    this.Div = document.createElement('div');
    this.Div.id = 'loading';
    this.Div.innerHTML = `
    <div class="loading"><div class="loading-content"><div class="ant-spin ant-spin-lg ant-spin-spinning ant-spin-show-text spin"><span class="ant-spin-dot ant-spin-dot-spin"><i class="ant-spin-dot-item"></i><i class="ant-spin-dot-item"></i><i class="ant-spin-dot-item"></i><i class="ant-spin-dot-item"></i></span><div class="ant-spin-text">加载中。。。</div></div></div></div>
    `
    if (timeout > 0) {
     if (this.timer) {
      clearInterval(this.timer)
     }
      this.timer = setInterval(() => {
        this.getHttpRequestTimer(timeout)
      }, 300)
    } else {
      const loading = document.getElementById('loading');
      if (!loading) {
        document.body.appendChild(this.Div);
      }
    }
  }
  public closeLoading() {
    const loading = document.getElementById('loading');
    clearInterval(this.timer)
    if (loading) {
      loading.remove();
    }
  }
  private getHttpRequestTimer(timeout: Number = 0) {
    this.httpRequestTimer = new Date().getTime()
    if ((this.httpRequestTimer - this.nowTime) > timeout) {
      const loading = document.getElementById('loading');
      if (!loading) {
        document.body.appendChild(this.Div);
      }
    }
  }
}